package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.order.entity.OrderAddress;
import com.ibiaoke.order.entity.vo.OrderAddressVO;
import com.ibiaoke.order.service.OrderAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by SuXin on 2017/6/22.
 */
@Controller
public class WxOrderAddressController extends BaseController {

    @Autowired
    private OrderAddressService orderAddressService;


    /**
     * 保存收货地址信息
     *
     * @param orderAddressVO
     * @return res
     */
    @RequestMapping("saveOrderAddress")
    @ResponseBody
    public Res saveOrderAddress(@Validated OrderAddressVO orderAddressVO, BindingResult result ) {
        handleValidate(result);
        Customer customer = getLoginUser();
        orderAddressVO.setUserId(customer.getCustomerId());
        orderAddressService.saveOrderAddress(orderAddressVO);
        return Res.create();
    }


    /**
     * 更改收货地址信息
     *
     * @param orderAddressVO
     * @return res  
     */
    @RequestMapping("/updateOrderAddress")
    @ResponseBody
    public Res updateOrderAddress(OrderAddressVO orderAddressVO) {
        Customer customer = getLoginUser();
        orderAddressVO.setUserId(customer.getCustomerId());
        orderAddressService.updateOrderAddress(orderAddressVO);
        return Res.create();
    }

    /**
     * 查询地址列表
     *
     * @return
     */
    @RequestMapping("/listOrderAddress")
    @ResponseBody
    public Res listOrderAddress() {
        Customer customer = getLoginUser();
        List<OrderAddress> list = orderAddressService.listOrderAddress(customer.getCustomerId());
        return Res.create().addResponse("orderAddressList", list);
    }

    /**
     * 获取单个地址信息
     * @param addressId
     * @return res
     */
    @RequestMapping("/getOrderAddress")
    @ResponseBody
    public Res getOrderAddress(Integer addressId) {
        OrderAddress orderAddress = orderAddressService.getOrderAddress(addressId);
        return Res.create().addResponse("orderAddress", orderAddress);
    }


    /**
     *删除收货地址
     * @param addressId
     * @return
     */
    @RequestMapping("/deleteOrderAddress")
    @ResponseBody
    public Res deleteOrderAddress(Integer addressId) {
        orderAddressService.deleteOrderAddress(addressId);
        return Res.create();
    }
}
